package cn.zh.stan.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.util.Loading;
import cn.zh.stan.util.RegexUtils;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class PayPass extends BaseActivity {

    MyApplication mapp;
    String phone;
    ImageView payps_back;
    String uid;

    Button reout_time,pstn;
    Mycount mycount;
    private Dialog LDialog;
    EditText recpass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_pass);
        mapp= (MyApplication) getApplication();
        phone=mapp.getSp().getString("phone","");
        System.out.println("------------------>phone===="+phone);
        uid=mapp.getSp().getString("uid","");
        initvi();
        }


      public void initvi(){
          payps_back= (ImageView) findViewById(R.id.payps_back);
          payps_back.setOnClickListener(this);
          reout_time= (Button) findViewById(R.id.psout_time);
          recpass= (EditText) findViewById(R.id.recpass);
          pstn= (Button) findViewById(R.id.pstn);
          mycount = new Mycount(60*1000,1000);
          mycount.start();
          reout_time.setEnabled(false);
          getPost();
          reout_time.setOnClickListener(this);
          pstn.setOnClickListener(this);
          }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.payps_back:
                finish();
                break;
            case R.id.psout_time:
                reout_time.setEnabled(false);
                if (RegexUtils.checkMobile(phone)){
                    LDialog= Loading.createLoadingDialog(this,"发送中...");
                    getPost();
                }else {
                    ToastUtil.showLong(PayPass.this,"请完善用户资料");
                  }
                break;
            case R.id.pstn:
                getcheck();
                break;
           }
        }


    public class Mycount extends CountDownTimer {
        public Mycount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onTick(long millisUntilFinished) {
            long myhour = (millisUntilFinished / 1000) / 3600;
            long myminute = ((millisUntilFinished / 1000) - myhour * 3600) / 60;
            long mysecond = millisUntilFinished / 1000 - myhour * 3600
                    - myminute * 60;
            reout_time.setText(mysecond+"后重发");
           }

        @Override
        public void onFinish() {
            reout_time.setText("重新发送");
            reout_time.setEnabled(true);
           }
         }


   //发送验证码

    public void getPost(){
          OkHttpUtils.post(BaseUrl.PayPwdSms)
                  .params("moblie",phone)
                  .execute(new StringCallback() {
                      @Override
                      public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                          System.out.println("------------>发送验证码===="+s);
                          Loading.closeDialog(LDialog);
                          reout_time.setEnabled(true);
                        }
                      @Override
                      public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                          super.onError(isFromCache, call, response, e);
                       }
                     });
                  }

        //检核验证码是否正确
  public void getcheck(){
      System.out.println("-------------。uid===="+uid);
        String code=recpass.getText().toString();
        OkHttpUtils.post(BaseUrl.PayPwdCheck)
                .params("username",phone)
                .params("code",code)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("--------->验证成功=="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                              boolean Flag=js.getBoolean("Flag");
                              if (Flag){
                                 Intent intent=new Intent(PayPass.this,ChagePs.class);
                                 startActivity(intent);
                                 finish();
                              }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
                }







   }
